import Vue from "vue";
import VueRouter from "vue-router";
import findLast from "lodash/findLast";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { check, isLogin } from "../utils/auth";
import AddClass from '../views/class/classChild/tab/tab1'

//这种方案需要所有角色都有一个共同的进入默认首页

Vue.use(VueRouter);
/**
 *hideInMenu    //不显示在菜单内容
 meta:
    --authority //权限
    --title:    "菜单标题内容",
    --icon      菜单图标
    --breadcrumb: false   //面包屑导航需不需要显示出来，有这条就是不显示出来
    --routerParent       //这里是子路由页面，选中父tab高亮
hideChildrenInMenu  //隐藏子路由不显示在菜单上，里面的子级路由
router 定义是有规则的，父级和子级名字最好前面都跟着相同的标识
 *
 * **/

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

const routes = [
  {
    path: "/user",
    hideInMenu: true, //不显示在菜单内容
    // component:{render: h=>h('router-view')},   //router-view点位
    component: () => import("../layouts/UserLayout"),
    children: [
      {
        path: "/user",
        redirect: "/user/login"
      },
      {
        path: "/user/login",
        name: "login",
        component: () => import("../views/user/Login")
      }
    ]
  },
  {
    path: "/",
    meta: { authority: ["admin","user"] },
    component: () => import("../layouts/Home"),
    
    children: [
      {
        path:'/',
        redirect:'/class'
      },
      // class 左侧菜单第一大级分类
      {
        path: "/class",
        name: "class",
        meta: {
          icon: "el-icon-location",
          title: "课程",
          breadcrumb: false
        },
        component: { render: h => h("router-view") },
        children: [ 
          //二级菜单
          {
            path: "/class",
            name: "class",
            meta: { title: "上传课程" },
            component: () => import("../layouts/Class"),
            children:[
              //
              {
                path: "",
                component: () => import("../views/class/Class"),
              },
              {
                path: "/class/add_class",
                name: "add_class",
                meta: { title: "课程信息",routerParent: 'add_class' },
                component: () => import("../views/class/classChild/AddClass"),
                children:[
                  {
                    path: "",
                    components:{
                      AddClass:AddClass
                    }
                  },
                  {
                    path: "/add_class/detail",
                    name: "detail",
                    meta: { title: "课程信息详情",routerParent: 'add_class'},
                    component: () => import("../views/class/classChild/detail/AddDetail"),
                  }
                ]
              },
              {
                path: "/class/danyuan_class",
                name: "danyuan_class",
                meta: { title: "单元信息",routerParent: 'danyuan_class' },
                component: () => import("../views/class/classChild/AddClass"),
                children:[
                  {
                    path: "",
                    component: () => import("../views/class/classChild/tab/tab2"),
                  },
                  {
                    path: "/danyuan_class/danyuan_detail",
                    name: "danyuan_detail",
                    meta: { title: "单元信息详情",routerParent: 'danyuan_class'},
                    component: () => import("../views/class/classChild/detail/DanyuanDetail"),
                  }
                ]
              },
              {
                path: "/class/keshi_class", 
                name: "keshi_class",
                meta: { title: "课时信息" ,routerParent: 'keshi_class' },
                component: () => import("../views/class/classChild/AddClass"),
                children:[
                  {
                    path: "",
                    component: () => import("../views/class/classChild/tab/tab3"),
                  },
                  {
                    path: "/keshi_class/keshi_detail",
                    name: "keshi_detail",
                    meta: { title: "单元信息详情"  ,routerParent: 'keshi_class'},
                    component: () => import("../views/class/classChild/detail/KeshiDetail"),
                  }
                ]
              },
              
            ]
          },
          {
            path: "/organ",
            name: "organ",
            meta: { title: "机构课程商品" },
            component: () => import("../views/orgen/Orgen"),
          }
        ]
      },
      // form
      {
        path: "/form",
        name: "form",
        component: { render: h => h("router-view") },
        meta: {
          icon: "el-icon-menu",
          title: "表单",
          authority: ["user"],
          breadcrumb: false
        },
        // hideChildrenInMenu: true,
        children: [
          {
            path: "/form/basic-form",
            name: "basicform",
            meta: { title: "基础表单" },
            component: () => import("../views/forms/TasicForm")
          },
          {
            path: "/form/step-form",
            name: "stepform",
            meta: { title: "分布表单" },
            hideChildrenInMenu: true,
            component: () => import("../views/forms/StepForm"),
            children: [
              {
                path: "",
                component: () => import("../views/forms/stepChild/info"),
              },
              {
                path: "/form/info_detail",
                name: "info_detail",
                meta: { title: "列表详情" },
                component: () => import("../views/forms/stepChild/infoDetail"),
              },
              {
                path: "/form/info_see",
                name: "info_see",
                meta: { title: "查看" },
                component: () => import("../layouts/Small"),
                children: [
                  {
                    path: "",
                    component: () => import("../views/forms/stepChild/See"),
                  },
                  {
                    path: "/form/3",
                    name: "small",
                    meta: { title: "3" },
                    component: () =>
                      import("../views/forms/stepChild/small.vue")
                  }
                ]
              },
            ]
          }
        ]
      },
      {
        path: "/test",
        name: "test",
        meta: { icon: "el-icon-menu", title: "测试",authority: ["admin"], breadcrumb: false },
        component: () => import("../views/Test")
      },
      {
        path: "/403",
        name: "403",
        hideInMenu: true, //不需要显示在菜单上
        component: () =>
          import(/* webpackChunkName: "exception" */ "../views/exception/403")
      },
      {
        path: "*",
        name: "404",
        hideInMenu: true,
        component: () =>
          import(/* webpackChunkName: "exception" */ "../views/exception/404")
      }
    ]
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  if (to.path !== from.path) {
    NProgress.start();
  }
  const record = findLast(to.matched, record => record.meta.authority);
  if (record && !check(record.meta.authority)) {
    if (!isLogin() && to.path !== "/user/login") {
      next({
        path: "/user/login"
      });
    }
    NProgress.done();
  }
  next();
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
